

import javax.swing.*;
import javax.swing.border.BevelBorder;
import javax.swing.border.Border;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 * Created by 蜡笔小新丶 on 2018/4/3.
 */
public class MyPanel extends JPanel implements ActionListener {
    private JLabel image, yname;
    private JTextArea remark;
    private JButton buy;
    Jade j;
    buy b;
    JFrame F;

    public JButton getBuy() {
        return buy;
    }

    public MyPanel(Jade jade,JFrame f) {
        //创建内容标签
        j = jade;
        F = f;
        image = new JLabel();
        yname = new JLabel();
        remark = new JTextArea();

        //创建购买按钮
        if(!ExInform.cha(j.getID())) {
            buy = new JButton("立即购买");
            buy.addActionListener(this);
        }else {
           buy = new JButton("商品已售出");
        }
        //添加内容
        byte[] arr = jade.getImage();//显示图片的二进制数组
        ImageIcon imageIcon = new ImageIcon(arr);
        imageIcon.setImage(imageIcon.getImage().getScaledInstance(1024 / 5 - 10, 650 / 3, Image.SCALE_DEFAULT));
        image.setIcon(imageIcon);
        image.setPreferredSize(new Dimension(1024 / 5 - 10, 650 / 3));
        yname.setText(jade.getYname());
        yname.setPreferredSize(new Dimension(1024 / 5 - 20, 30));
        remark.setText(jade.getRemark());
        remark.setPreferredSize(new Dimension(1024 / 5 - 20, 45));
        remark.setBackground(Color.white);

        this.add(image);
        this.add(yname);
        this.add(remark);
        this.add(buy);

        this.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
        this.setBorder(BorderFactory.createLineBorder(Color.gray, 1));

    }

    public void setColour(Color color) {
        this.setBackground(color);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getActionCommand() == "立即购买") {
            if (ExInform.getU() != null) {
                if (!ExInform.getU().getUserNum().equals(j.getNum())) {
                    if (ExInform.getU().getMoney() < Integer.parseInt(j.getMoney())) {
                        JOptionPane.showMessageDialog(null, "余额不足！", "提示消息", JOptionPane.WARNING_MESSAGE);
                    } else {
                        String str = "您想要购买：" + j.getYname() + "吗？" + "\n" + "价格：" + j.getMoney();
                        int A = JOptionPane.showConfirmDialog(null, str, "提示消息！", JOptionPane.WARNING_MESSAGE);
                        if(A==0) {
                            b = new buy(ExInform.getU().getUserNum(), j.getID());
                            ExInform.bought(b);
                            JOptionPane.showConfirmDialog(null, "购买成功！", "提示消息！", JOptionPane.WARNING_MESSAGE);
                            F.dispose();
                            new Home(ExInform.getU());
                        }
                    }
                } else {
                    JOptionPane.showMessageDialog(null, "该物品为您所有！", "提示消息", JOptionPane.WARNING_MESSAGE);
                }
            } else {
                JOptionPane.showMessageDialog(null, "不可购买，请先登录！", "提示消息", JOptionPane.WARNING_MESSAGE);
            }
        }
    }
}
